// pages/archives/archives.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    edit: true,
    archivesArr: [],
    num: "0"
  },
  edit: function() {
    this.setData({
      edit: !this.data.edit
    })
  },
  guanxi: function(e) {
    var that = this
    that.setData({
      j_id1: e.currentTarget.dataset.j_id
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    //接上个页面的数据
    var conArr = JSON.parse(options.conArr) 
    var zhichengArr = JSON.parse(options.zhichengArrJson)
    console.log(conArr, options.types)
    that.setData({
      bgc: bgc,
      conArr: conArr,
      zhichengArr: zhichengArr,
      types: options.types
    })
    //计算年龄
    var archivesArr = that.data.archivesArr
    //初始化时 没档案自动跳入新建档案
    // if (archivesArr.length == 0) {
    //   wx.navigateTo({
    //     url: '../../pages/new/new',
    //   })
    // }
    var date = new Date;
    var year = date.getFullYear();
    console.log(year)

    for (var i = 0, len = archivesArr.length; i < len; i++) {
      var year2 = archivesArr[i].data.substr(0, 4)
      console.log(year2)
      archivesArr[i].age = year - year2
    }
    that.setData({
      archivesArr: archivesArr
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.getAlldangan()
    this.getAllzhuanjia()
  },
  //选择哪个档案
  chooseThis: function(e) {
    var that = this
    var index = e.currentTarget.dataset.index
    that.setData({
      num: index
    })
  },
  //删除档案
  delArchives: function(e) {
    var that = this
    var index = e.currentTarget.dataset.index
    if (index == 0) {
      wx.showToast({
        title: '该档案不能删除',
      })
    } else {
      var archivesArr = that.data.archivesArr
      wx.showModal({
        title: '确定要删除该档案',
        success: function(res) {
          if (res.confirm) {
            archivesArr.splice(index, 1)
            that.setData({
              archivesArr: archivesArr
            })
          }
        }
      })
    }
  },
  //添加档案
  addArchives: function() {
    wx.navigateTo({
      url: '../../pages/new/new',
    })
  },
  //完成
  complete: function() {
    var that =this
    var conArr = JSON.stringify(that.data.conArr) 
    var zhichengArr = JSON.stringify(that.data.zhichengArr)
    if (that.data.j_id1 !== undefined){
      var j_id = that.data.j_id1
    }else{
      var j_id = that.data.archivesArr[0].j_id
    }
    //模糊查询医生

    wx.navigateTo({
      url: '../../pages/doctor/doctor?j_id=' + j_id + "&conArr=" + conArr + "&zhichengArr=" + zhichengArr + '&types=' + that.data.types,
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getAlldangan()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  getAlldangan: function() {
    var that = this
    app.util.request({
      'url': 'entry/wxapp/Addtijian',
      data: {
        op: 'display',
        openid: wx.getStorageSync('openid')
      },
      success: function(res) {
        console.log(res)
        that.setData({
          archivesArr: res.data
        })
      }
    })
  },
  getAllzhuanjia:function(){
    var that =this
    app.util.request({
      'url': 'entry/wxapp/Zhuanjia',
      success: function (res) {
        console.log(res)
        var info = res.data.data
        that.setData({
          zhuanjia: info,
        });
      },
      fail: function (err) {
        console.log(err)
      },
    })
  }
})